import 'package:cached_network_image/cached_network_image.dart';
import 'package:casdon/app/config/theme/theme_extention.dart';
import 'package:casdon/build_gen/assets.gen.dart';
import 'package:casdon/common/widget/badge_icon.dart';
import 'package:casdon/modules/recipe/data/model/recipe_placeholder_data.dart';
import 'package:casdon/modules/recipe/ui/widgets/play_orverlay_button.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

abstract class RecipeCardData {

  int get sort;
  String get imgUrl;
  String get title;
  String get viewCount;
  String get recipeId;
  bool get isSmart;
  String? get url;
  String get count; // 菜谱集时有意义，菜谱时无意义
  bool get isVideo => false;

  // 占位用的，
  RecipeCardData placeholder();
}

class RecipeCardItem extends StatelessWidget {
  final RecipeCardData item;

  const RecipeCardItem({super.key, required this.item});

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        // 图片+智能标签+播放按钮
        SizedBox(
          height: 110.h,
          width: double.infinity,
          child: ClipRRect(
            borderRadius: BorderRadius.circular(8),
            child: Stack(
              fit: StackFit.expand,
              children: [
                CachedNetworkImage(
                  imageUrl: item.imgUrl,
                  fit: BoxFit.cover,
                  fadeInDuration: Duration.zero,
                  placeholder: (context, url) => const SizedBox(),
                  errorWidget: (context, url, error) {
                    return Image.network(
                      recipePlaceHolderImgUrl,
                      fit: BoxFit.cover,
                      errorBuilder: (context, error, stackTrace) {
                        return Container(
                          color: Colors.grey[200],
                          child: const Icon(Icons.image_outlined, color: Colors.grey),
                        );
                      },
                    );
                  },
                ),
                if (item.isVideo) const PlayOverlayButton(),
                if (item.isSmart)
                  Positioned(
                    right: 6,
                    top: 6,
                    child: Container(
                      padding: const EdgeInsets.symmetric(
                        horizontal: 6,
                        vertical: 2,
                      ),
                      decoration: BoxDecoration(
                        gradient: LinearGradient(
                          colors: [Color(0xFF9470FF), Color(0xFF6652F9)],
                          begin: Alignment.centerLeft,
                          end: Alignment.centerRight,
                        ),
                        borderRadius: BorderRadius.circular(4),
                      ),
                      child: const Text(
                        '智能',
                        style: TextStyle(color: Colors.white, fontSize: 10),
                      ),
                    ),
                  ),
              ],
            ),
          ),
        ),
        const SizedBox(height: 6),
        // 标题与浏览数
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            SizedBox(width: 4.w),
            Expanded(
              child: Text(
                item.title,
                style: context.theme.textTheme.bodyMedium!.copyWith(
                  color: context.customColorThemeNonNull.textColor1,
                ),
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
              ),
            ),
            Row(
              children: [
                BadgeIcon(imagePath: Assets.images.icView.path, size: 18.r),
                SizedBox(width: 2.w),
                Text(
                  item.viewCount,
                  style: context.theme.textTheme.bodySmall!.copyWith(
                    color: context.customColorThemeNonNull.textColor2,
                  ),
                ),
                SizedBox(width: 4.w),
              ],
            ),
          ],
        ),
      ],
    );
  }
}
